<?php

namespace App\Constants;

/**
 * 文件类型枚举
 * 定义各种文件类型和处理方式
 */
class FileTypeEnum extends AbstractEnum
{
    // 文件类型常量
    public const IMAGE = 'image';
    public const JPEG = 'jpeg';
    public const PNG = 'png';
    public const GIF = 'gif';
    public const WEBP = 'webp';
    public const SVG = 'svg';
    public const BMP = 'bmp';
    public const TIFF = 'tiff';

    public const DOCUMENT = 'document';
    public const DOC = 'doc';
    public const DOCX = 'docx';
    public const TXT = 'txt';
    public const RTF = 'rtf';
    public const ODT = 'odt';

    public const SPREADSHEET = 'spreadsheet';
    public const XLS = 'xls';
    public const XLSX = 'xlsx';
    public const CSV = 'csv';
    public const ODS = 'ods';

    public const PRESENTATION = 'presentation';
    public const PPT = 'ppt';
    public const PPTX = 'pptx';
    public const ODP = 'odp';

    public const PDF = 'pdf';

    public const ARCHIVE = 'archive';
    public const ZIP = 'zip';
    public const RAR = 'rar';
    public const TAR = 'tar';
    public const GZ = 'gz';
    public const BZ2 = 'bz2';
    public const XZ = 'xz';

    public const AUDIO = 'audio';
    public const MP3 = 'mp3';
    public const WAV = 'wav';
    public const FLAC = 'flac';
    public const AAC = 'aac';
    public const OGG = 'ogg';

    public const VIDEO = 'video';
    public const MP4 = 'mp4';
    public const AVI = 'avi';
    public const MOV = 'mov';
    public const WMV = 'wmv';
    public const FLV = 'flv';
    public const WEBM = 'webm';
    public const MKV = 'mkv';

    public const OTHER = 'other';

    /**
     * 获取所有枚举实例
     */
    public static function getAll(): array
    {
        return [
            new self('image', '图片', '图片文件', '#28A745', 'image', 1),
            new self('document', '文档', '文本文档', '#17A2B8', 'file-word', 2),
            new self('spreadsheet', '表格', '电子表格', '#28A745', 'file-excel', 3),
            new self('presentation', '演示文稿', 'PPT演示文稿', '#DC3545', 'file-powerpoint', 4),
            new self('pdf', 'PDF', 'PDF文档', '#DC3545', 'file-pdf', 5),
            new self('archive', '压缩包', '压缩文件', '#6C757D', 'file-archive', 6),
            new self('audio', '音频', '音频文件', '#20C997', 'file-audio', 7),
            new self('video', '视频', '视频文件', '#FFC107', 'file-video', 8),
            new self('other', '其他', '其他文件', '#6F42C1', 'file', 9),
        ];
    }



    /**
     * 获取可编辑的文件类型
     */
    public static function getEditableTypes(): array
    {
        return [
            self::DOCUMENT,
            self::SPREADSHEET,
            self::PRESENTATION,
        ];
    }

    /**
     * 获取媒体文件类型
     */
    public static function getMediaTypes(): array
    {
        return [
            self::IMAGE,
            self::AUDIO,
            self::VIDEO,
        ];
    }

    /**
     * 获取办公文件类型
     */
    public static function getOfficeTypes(): array
    {
        return [
            self::DOCUMENT,
            self::SPREADSHEET,
            self::PRESENTATION,
            self::PDF,
        ];
    }



    /**
     * 检查是否为可编辑的文件
     */
    public static function isEditable(string $type): bool
    {
        return in_array($type, self::getEditableTypes());
    }

    /**
     * 检查是否为媒体文件
     */
    public static function isMediaFile(string $type): bool
    {
        return in_array($type, self::getMediaTypes());
    }

    /**
     * 检查是否为办公文件
     */
    public static function isOfficeFile(string $type): bool
    {
        return in_array($type, self::getOfficeTypes());
    }

    /**
     * 根据文件扩展名获取文件类型
     */
    public static function getTypeByExtension(string $extension): string
    {
        $extension = strtolower($extension);

        $typeMap = [
            // 图片
            'jpg' => self::JPEG, 'jpeg' => self::JPEG, 'png' => self::PNG,
            'gif' => self::GIF, 'bmp' => self::BMP, 'webp' => self::WEBP,
            'svg' => self::SVG, 'tiff' => self::TIFF, 'tif' => self::TIFF,

            // 文档
            'doc' => self::DOC, 'docx' => self::DOCX, 'txt' => self::TXT,
            'rtf' => self::RTF, 'odt' => self::ODT,

            // 表格
            'xls' => self::XLS, 'xlsx' => self::XLSX, 'csv' => self::CSV,
            'ods' => self::ODS,

            // 演示文稿
            'ppt' => self::PPT, 'pptx' => self::PPTX, 'odp' => self::ODP,

            // PDF
            'pdf' => self::PDF,

            // 压缩包
            'zip' => self::ZIP, 'rar' => self::RAR, '7z' => self::ARCHIVE,
            'tar' => self::TAR, 'gz' => self::GZ, 'bz2' => self::BZ2,
            'xz' => self::XZ,

            // 音频
            'mp3' => self::MP3, 'wav' => self::WAV, 'flac' => self::FLAC,
            'aac' => self::AAC, 'ogg' => self::OGG, 'm4a' => self::AAC,

            // 视频
            'mp4' => self::MP4, 'avi' => self::AVI, 'mkv' => self::MKV,
            'mov' => self::MOV, 'wmv' => self::WMV, 'flv' => self::FLV,
            'webm' => self::WEBM,
        ];

        return $typeMap[$extension] ?? self::OTHER;
    }

    /**
     * 根据MIME类型获取文件类型
     */
    public static function getTypeByMimeType(string $mimeType): string
    {
        $mimeType = strtolower($mimeType);

        $mimeMap = [
            // 图片
            'image/jpeg' => self::JPEG,
            'image/png' => self::PNG,
            'image/gif' => self::GIF,
            'image/webp' => self::WEBP,
            'image/svg+xml' => self::SVG,
            'image/bmp' => self::BMP,
            'image/tiff' => self::TIFF,

            // 文档
            'application/msword' => self::DOC,
            'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => self::DOCX,
            'text/plain' => self::TXT,
            'application/rtf' => self::RTF,
            'application/vnd.oasis.opendocument.text' => self::ODT,

            // 表格
            'application/vnd.ms-excel' => self::XLS,
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => self::XLSX,
            'text/csv' => self::CSV,
            'application/vnd.oasis.opendocument.spreadsheet' => self::ODS,

            // 演示文稿
            'application/vnd.ms-powerpoint' => self::PPT,
            'application/vnd.openxmlformats-officedocument.presentationml.presentation' => self::PPTX,
            'application/vnd.oasis.opendocument.presentation' => self::ODP,

            // PDF
            'application/pdf' => self::PDF,

            // 压缩包
            'application/zip' => self::ZIP,
            'application/x-zip-compressed' => self::ZIP,
            'application/x-rar-compressed' => self::RAR,
            'application/x-tar' => self::TAR,
            'application/gzip' => self::GZ,
            'application/x-bzip2' => self::BZ2,
            'application/x-xz' => self::XZ,

            // 音频
            'audio/mpeg' => self::MP3,
            'audio/mp3' => self::MP3,
            'audio/wav' => self::WAV,
            'audio/x-wav' => self::WAV,
            'audio/flac' => self::FLAC,
            'audio/aac' => self::AAC,
            'audio/ogg' => self::OGG,
            'audio/mp4' => self::AAC,

            // 视频
            'video/mp4' => self::MP4,
            'video/avi' => self::AVI,
            'video/quicktime' => self::MOV,
            'video/x-ms-wmv' => self::WMV,
            'video/x-flv' => self::FLV,
            'video/webm' => self::WEBM,
            'video/x-matroska' => self::MKV,
        ];

        return $mimeMap[$mimeType] ?? self::OTHER;
    }

    /**
     * 获取文件类型的最大大小限制
     */
    public static function getMaxSize(string $type): int
    {
        $sizeMap = [
            // 图片
            self::JPEG => 10 * 1024 * 1024, // 10MB
            self::PNG => 10 * 1024 * 1024,
            self::GIF => 10 * 1024 * 1024,
            self::WEBP => 10 * 1024 * 1024,
            self::SVG => 5 * 1024 * 1024, // 5MB
            self::BMP => 20 * 1024 * 1024,
            self::TIFF => 50 * 1024 * 1024,

            // 文档
            self::DOC => 50 * 1024 * 1024, // 50MB
            self::DOCX => 50 * 1024 * 1024,
            self::TXT => 10 * 1024 * 1024,
            self::RTF => 20 * 1024 * 1024,
            self::ODT => 50 * 1024 * 1024,

            // 表格
            self::XLS => 50 * 1024 * 1024,
            self::XLSX => 50 * 1024 * 1024,
            self::CSV => 20 * 1024 * 1024,
            self::ODS => 50 * 1024 * 1024,

            // 演示文稿
            self::PPT => 100 * 1024 * 1024, // 100MB
            self::PPTX => 100 * 1024 * 1024,
            self::ODP => 100 * 1024 * 1024,

            // PDF
            self::PDF => 50 * 1024 * 1024,

            // 压缩包
            self::ZIP => 500 * 1024 * 1024, // 500MB
            self::RAR => 500 * 1024 * 1024,
            self::TAR => 200 * 1024 * 1024,
            self::GZ => 200 * 1024 * 1024,
            self::BZ2 => 200 * 1024 * 1024,
            self::XZ => 200 * 1024 * 1024,

            // 音频
            self::MP3 => 50 * 1024 * 1024,
            self::WAV => 100 * 1024 * 1024,
            self::FLAC => 100 * 1024 * 1024,
            self::AAC => 50 * 1024 * 1024,
            self::OGG => 50 * 1024 * 1024,

            // 视频
            self::MP4 => 500 * 1024 * 1024, // 500MB
            self::AVI => 500 * 1024 * 1024,
            self::MOV => 500 * 1024 * 1024,
            self::WMV => 500 * 1024 * 1024,
            self::FLV => 200 * 1024 * 1024,
            self::WEBM => 500 * 1024 * 1024,
            self::MKV => 500 * 1024 * 1024,

            // 其他
            self::OTHER => 10 * 1024 * 1024,
        ];

        return $sizeMap[$type] ?? 10 * 1024 * 1024; // 默认10MB
    }

    /**
     * 检查文件类型是否为图片
     */
    public static function isImage(string $type): bool
    {
        $imageTypes = [self::JPEG, self::PNG, self::GIF, self::WEBP, self::SVG, self::BMP, self::TIFF];
        return in_array($type, $imageTypes);
    }

    /**
     * 检查文件类型是否为文档
     */
    public static function isDocument(string $type): bool
    {
        $documentTypes = [self::DOC, self::DOCX, self::TXT, self::RTF, self::ODT];
        return in_array($type, $documentTypes);
    }

    /**
     * 检查文件类型是否为办公文件
     */
    public static function isOffice(string $type): bool
    {
        $officeTypes = [
            self::DOC, self::DOCX, self::XLS, self::XLSX, self::PPT, self::PPTX,
            self::PDF, self::TXT, self::CSV, self::RTF, self::ODT, self::ODS, self::ODP
        ];
        return in_array($type, $officeTypes);
    }

    /**
     * 检查文件类型是否为媒体文件
     */
    public static function isMedia(string $type): bool
    {
        $mediaTypes = [self::MP3, self::WAV, self::FLAC, self::AAC, self::OGG, self::MP4, self::AVI, self::MOV, self::WMV, self::FLV, self::WEBM, self::MKV];
        return in_array($type, $mediaTypes);
    }

    /**
     * 获取可预览的文件类型
     */
    public static function getPreviewableTypes(): array
    {
        return [
            self::JPEG, self::PNG, self::GIF, self::WEBP, self::SVG, self::BMP, self::TIFF,
            self::PDF, self::TXT, self::DOC, self::DOCX,
            self::MP4, self::WEBM, self::MOV
        ];
    }

    /**
     * 检查文件类型是否支持预览
     */
    public static function isPreviewable(string $type): bool
    {
        return in_array($type, self::getPreviewableTypes());
    }

    /**
     * 获取文件类型的MIME类型
     */
    public static function getMimeType(string $type): string
    {
        $mimeMap = [
            // 图片
            self::JPEG => 'image/jpeg',
            self::PNG => 'image/png',
            self::GIF => 'image/gif',
            self::WEBP => 'image/webp',
            self::SVG => 'image/svg+xml',
            self::BMP => 'image/bmp',
            self::TIFF => 'image/tiff',

            // 文档
            self::DOC => 'application/msword',
            self::DOCX => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
            self::TXT => 'text/plain',
            self::RTF => 'application/rtf',
            self::ODT => 'application/vnd.oasis.opendocument.text',

            // 表格
            self::XLS => 'application/vnd.ms-excel',
            self::XLSX => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            self::CSV => 'text/csv',
            self::ODS => 'application/vnd.oasis.opendocument.spreadsheet',

            // 演示文稿
            self::PPT => 'application/vnd.ms-powerpoint',
            self::PPTX => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
            self::ODP => 'application/vnd.oasis.opendocument.presentation',

            // PDF
            self::PDF => 'application/pdf',

            // 压缩包
            self::ZIP => 'application/zip',
            self::RAR => 'application/x-rar-compressed',
            self::TAR => 'application/x-tar',
            self::GZ => 'application/gzip',
            self::BZ2 => 'application/x-bzip2',
            self::XZ => 'application/x-xz',

            // 音频
            self::MP3 => 'audio/mpeg',
            self::WAV => 'audio/wav',
            self::FLAC => 'audio/flac',
            self::AAC => 'audio/aac',
            self::OGG => 'audio/ogg',

            // 视频
            self::MP4 => 'video/mp4',
            self::AVI => 'video/avi',
            self::MOV => 'video/quicktime',
            self::WMV => 'video/x-ms-wmv',
            self::FLV => 'video/x-flv',
            self::WEBM => 'video/webm',
            self::MKV => 'video/x-matroska',
        ];

        return $mimeMap[$type] ?? 'application/octet-stream';
    }
}
